package com.caigou.modules.famous.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.view.Window
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.ext.dareTypeName
import com.caigou.modules.famous.R
import com.caigou.modules.famous.bean.ChallengeParamsItemBean
import com.caigou.modules.famous.bean.ChallengeParamsMoneyItemBean
import com.caigou.modules.famous.bean.ChallengeParamsSuccessItemBean

/**
 * @author #Suyghur.
 * Created on 2021/12/12
 */
class JoinChallengeDialog(context: Context, private val dareTypeId: Int, private val paramsList: MutableList<ChallengeParamsItemBean>) :
    Dialog(context) {

    private lateinit var tvTitle: TextView

    private lateinit var rgPeriod: RadioGroup
    private lateinit var rgDifficult: RadioGroup
    private lateinit var rgLevel: RadioGroup

    private lateinit var tvReward: TextView

    private lateinit var btnPeriod1: RadioButton
    private lateinit var btnPeriod2: RadioButton
    private lateinit var btnPeriod3: RadioButton

    private lateinit var btnDifficult1: RadioButton
    private lateinit var btnDifficult2: RadioButton
    private lateinit var btnDifficult3: RadioButton

    private lateinit var btnLevel1: RadioButton
    private lateinit var btnLevel2: RadioButton
    private lateinit var btnLevel3: RadioButton

    private lateinit var btnCancel: Button
    private lateinit var btnRule: Button
    lateinit var btnJoin: Button

    private val btnPeriodList = mutableListOf<RadioButton>()
    private val btnDifficultList = mutableListOf<RadioButton>()
    private val btnLevelList = mutableListOf<RadioButton>()


    private var periodIndex = 0
    private var difficultIndex = 0
    private var levelIndex = 0

    init {
        setCanceledOnTouchOutside(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
    }

    private fun initView() {
        setContentView(R.layout.dialog_join_challenge)

        tvTitle = findViewById(R.id.join_challenge_tv_title)
        tvTitle.text = dareTypeName(dareTypeId)

        rgPeriod = findViewById(R.id.join_challenge_rg_period_selector)
        rgPeriod.setOnCheckedChangeListener { _, checkedId ->
            periodIndex = 0
            difficultIndex = 0
            levelIndex = 0
            periodIndex = when (checkedId) {
                R.id.join_challenge_btn_period_1 -> 0
                R.id.join_challenge_btn_period_2 -> 1
                R.id.join_challenge_btn_period_3 -> 2
                else -> 0
            }
            changeDifficultBtn(paramsList[periodIndex].successList)
            changeRewardTxt()
        }

        rgDifficult = findViewById(R.id.join_challenge_rg_difficult_selector)
        rgDifficult.setOnCheckedChangeListener { _, checkedId ->
            periodIndex = 0
            difficultIndex = 0
            levelIndex = 0
            difficultIndex = when (checkedId) {
                R.id.join_challenge_btn_difficult_1 -> 0
                R.id.join_challenge_btn_difficult_2 -> 1
                R.id.join_challenge_btn_difficult_3 -> 2
                else -> 0
            }
            changeLevelBtn(paramsList[periodIndex].successList[difficultIndex].moneyList)
            changeRewardTxt()
        }

        rgLevel = findViewById(R.id.join_challenge_rg_level_selector)
        rgLevel.setOnCheckedChangeListener { _, checkedId ->
            periodIndex = 0
            difficultIndex = 0
            levelIndex = 0
            levelIndex = when (checkedId) {
                R.id.join_challenge_btn_level_1 -> 0
                R.id.join_challenge_btn_level_2 -> 1
                R.id.join_challenge_btn_level_3 -> 2
                else -> 0
            }
            changeRewardTxt()
        }

        btnPeriod1 = findViewById(R.id.join_challenge_btn_period_1)
        btnPeriod2 = findViewById(R.id.join_challenge_btn_period_2)
        btnPeriod3 = findViewById(R.id.join_challenge_btn_period_3)
        btnPeriodList.add(btnPeriod1)
        btnPeriodList.add(btnPeriod2)
        btnPeriodList.add(btnPeriod3)


        btnDifficult1 = findViewById(R.id.join_challenge_btn_difficult_1)
        btnDifficult2 = findViewById(R.id.join_challenge_btn_difficult_2)
        btnDifficult3 = findViewById(R.id.join_challenge_btn_difficult_3)
        btnDifficultList.add(btnDifficult1)
        btnDifficultList.add(btnDifficult2)
        btnDifficultList.add(btnDifficult3)


        btnLevel1 = findViewById(R.id.join_challenge_btn_level_1)
        btnLevel2 = findViewById(R.id.join_challenge_btn_level_2)
        btnLevel3 = findViewById(R.id.join_challenge_btn_level_3)
        btnLevelList.add(btnLevel1)
        btnLevelList.add(btnLevel2)
        btnLevelList.add(btnLevel3)

        tvReward = findViewById(R.id.join_challenge_tv_reward)

        btnCancel = findViewById(R.id.join_challenge_btn_cancel)
        btnCancel.setOnClickListener {
            dismiss()
        }

        btnRule = findViewById(R.id.join_challenge_btn_rule)
        btnRule.setOnClickListener {
            ARouter.getInstance().build(RouterPath.Famous.PAGE_RULE).navigation()
            dismiss()
        }

        btnJoin = findViewById(R.id.join_challenge_btn_join)


        changeRewardTxt()
        handlePeriodBtn()
    }

    private fun handlePeriodBtn() {
        for (index in 0 until paramsList.size) {
            with(btnPeriodList[index]) {
                visibility = View.VISIBLE
                text = "${paramsList[index].count}期"
            }
        }
        changeDifficultBtn(paramsList[0].successList)
        changeLevelBtn(paramsList[0].successList[0].moneyList)
    }

    private fun changeDifficultBtn(successList: MutableList<ChallengeParamsSuccessItemBean>) {
        btnDifficultList[0].isChecked = true
        btnLevelList[0].isChecked = true
        btnDifficultList.forEach {
            it.visibility = View.GONE
        }
        for (index in 0 until successList.size) {
            with(btnDifficultList[index]) {
                visibility = View.VISIBLE
                text = successList[index].countSuccessDesc
            }
        }
    }

    private fun changeLevelBtn(moneyList: MutableList<ChallengeParamsMoneyItemBean>) {
        btnLevelList[0].isChecked = true
        btnLevelList.forEach {
            it.visibility = View.GONE
        }
        for (index in 0 until moneyList.size) {
            with(btnLevelList[index]) {
                visibility = View.VISIBLE
                val moneyRule = moneyList[index].moneyRule
                text = if (moneyRule == 0) {
                    "免费"
                } else {
                    "${moneyRule}元"
                }

            }
        }
    }

    private fun changeRewardTxt() {
        val successList = paramsList[periodIndex].successList
        val moneyList = successList[difficultIndex].moneyList
        tvReward.text = "可获得${moneyList[levelIndex].moneyWin}元彩金"
    }

    fun currentMoneyId(): Int {
        return paramsList[periodIndex].successList[difficultIndex].moneyList[levelIndex].id
    }

    fun currentSuccessDesc(): String {
        return paramsList[periodIndex].successList[difficultIndex].countSuccessDesc
    }
}